package com.example.zhidingyiview.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.example.zhidingyiview.zhidingyiview.CircleProgressBar;
import com.example.zhidingyiview.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.cpb)
    CircleProgressBar mCpb;
    @BindView(R.id.btn)
    Button mBtn;
    @BindView(R.id.btn_tiao)
    Button mBtnTiao;
    private boolean down;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn, R.id.btn_tiao})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn:
                download();
                break;
            case R.id.btn_tiao:
                Intent intent = new Intent(MainActivity.this, BallViewActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    private void download() {
        if (!down) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 101; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        down = true;
                        if (i == 100) {
                            down = false;
                        }
                        mCpb.setProgress(i);
                    }
                }
            }).start();
        }
    }
}